﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TCM.Controllers
{
    public class SubjectController : Controller
    {
        private TCMContext db = new TCMContext();

        //
        // GET: /Subject/

        public ActionResult Index()
        {
            return View(db.Subjects.ToList());
        }

        //
        // GET: /Subject/Details/5

        public ActionResult Details(Guid? id = null)
        {
            Subject Subject = db.Subjects.Find(id);
            if (Subject == null)
            {
                return HttpNotFound();
            }
            return View(Subject);
        }

        //
        // GET: /Subject/Create

        public ActionResult Create()
        {
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName");

            ViewBag.SubjectCateogryID = new SelectList(db.SubjectCategories.ToList().AsEnumerable(), "SubjectCategoryID", "SubjectCategoryName");

            return View();
        }

        //
        // POST: /Subject/Create

        [HttpPost]
        public ActionResult Create(Subject Subject)
        {
            if (ModelState.IsValid)
            {
               // Subject.SubjectID = Guid.NewGuid();
                db.Subjects.Add(Subject);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable(), "StatusID", "StatusName");

            ViewBag.SubjeCategoryID = new SelectList(db.SubjectCategories
                .ToList().AsEnumerable(), "SubjeCategoryID", "SubjectCategoryName");
            return View(Subject);
        }

        //
        // GET: /Subject/Edit/5

        public ActionResult Edit(Guid? id = null)
        {
            Subject Subject = db.Subjects.Find(id);
            if (Subject == null)
            {
                return HttpNotFound();
            }
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName", Subject.StatusID );

            ViewBag.SubjectCateogryID = new SelectList(db.SubjectCategories
                .ToList().AsEnumerable(), "SubjectCategoryID", "SubjectCategoryName", Subject.SubjectCateogryID);

            return View(Subject);
        }

        //
        // POST: /Subject/Edit/5

        [HttpPost]
        public ActionResult Edit(Subject Subject)
        {
            if (ModelState.IsValid)
            {
                db.Entry(Subject).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName", Subject.StatusID);
          
            ViewBag.SubjectCateogryID = new SelectList(db.SubjectCategories.ToList().AsEnumerable(), "SubjectCategoryID", "SubjectCateogryName", Subject.SubjectCateogryID);
            return View(Subject);
        }

        //
        // GET: /Subject/Delete/5

        public ActionResult Delete(Guid? id = null)
        {
            Subject Subject = db.Subjects.Find(id);
            if (Subject == null)
            {
                return HttpNotFound();
            }
            return View(Subject);
        }

        //
        // POST: /Subject/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Subject Subject = db.Subjects.Find(id);
            db.Subjects.Remove(Subject);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}